package Chapter25.jdbc.preparedstatement_;

import com.mysql.jdbc.Connection;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 杨森
 * @date 2022年11月20日 14:43
 */
public class PreparedStatementEx {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入姓名");
        String name=sc.nextLine();
        System.out.println("请输入密码");
        String pwd=sc.nextLine();


        Properties pro = new Properties();
        pro.load(new FileReader("src//mysql.properties"));
        String user=pro.getProperty("user");
        String password = pro.getProperty("password");
        String driver=pro.getProperty("driver");
        String url = pro.getProperty("url");

        Class.forName(driver);

        Connection connection= (Connection) DriverManager.getConnection(url,user,password);

        String sql = "insert into admin values(?, ?)";

        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, pwd);

        int rows=preparedStatement.executeUpdate();
        System.out.println(rows > 0 ? "执行成功" : "执行失败");
        //关闭连接
        preparedStatement.close();
        connection.close();

    }
}
